package test.framework.config;

import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

/**
 * 动态数据源
 * @author: huwei
 * @date: 2019/9/11 14:31
 * @version: 1.3.0
 */
@Configuration
@EnableTransactionManagement
@MapperScan(value = {"test.module.**.mapper"} ,sqlSessionFactoryRef = "sqlSessionFactoryBean")
public class MultipleDataSourceConfig {

    /**
     * admin数据源配置
     * @return
     */
    @Primary
    @Bean(name = "datasource1")
    @ConfigurationProperties(prefix = "spring.datasource.datasource1")
    public DataSource getDateSource1() {
        return DataSourceBuilder.create().build();
    }

    /**
     * 其它数据源配置
     * @return
     */
    @Bean(name = "datasource2")
    @ConfigurationProperties(prefix = "spring.datasource.datasource2")
    public DataSource getDateSource2() {
        return DataSourceBuilder.create().build();
    }

    /**
     * 动态数据源配置，用于动态切换不同的数据源
     * @param dataSource1
     * @param dataSource2
     * @return
     */
    @Bean(name = "dynamicDataSource")
    public MultipleDataSource DataSource(@Qualifier("datasource1") DataSource dataSource1, @Qualifier("datasource2") DataSource dataSource2) {
        Map<Object, Object> targetDataSource = new HashMap<>(2);
        targetDataSource.put("datasource1", dataSource1);
        targetDataSource.put("datasource2", dataSource2);
        MultipleDataSource dataSource = new MultipleDataSource();
        dataSource.setTargetDataSources(targetDataSource);
        dataSource.setDefaultTargetDataSource(dataSource1);
        return dataSource;
    }

    /**
     * mybatis sql session 工厂配置
     * @param dynamicDataSource
     * @return
     * @throws Exception
     */
    @Bean(name = "sqlSessionFactoryBean")
    public MybatisSqlSessionFactoryBean sqlSessionFactoryBean(@Qualifier("dynamicDataSource") DataSource dynamicDataSource)
            throws Exception {
        MybatisSqlSessionFactoryBean sqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dynamicDataSource);
        sqlSessionFactoryBean.setMapperLocations(
                new PathMatchingResourcePatternResolver().getResources("classpath*:test/module/**/*.xml"));
        sqlSessionFactoryBean.setPlugins(paginationInterceptor());
        return sqlSessionFactoryBean;
    }

    /**
     * 分页插件
     * paginationInterceptor.setLimit(你的最大单页限制数量，默认 500 条，小于 0 如 -1 不受限制);
     */
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
        paginationInterceptor.setLimit(50);
        return paginationInterceptor;
    }
}
